package com.shujia.util;

import org.apache.commons.dbcp2.BasicDataSource;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class JDBCUtil {

   /* private static Connection con;

    static {
        System.out.println("创建数据库连接");
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //建立连接
            con = DriverManager.getConnection("jdbc:mysql://node3:3306/shujia?useUnicode=true&characterEncoding=utf-8", "root", "123456");

        } catch (Exception e) {
            e.printStackTrace();
        }

    }*/


    /**
     * 创建连接池
     */

    private static BasicDataSource dataSource;

    static {
        dataSource = new BasicDataSource();

        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://node3:3306/shujia?useUnicode=true&characterEncoding=utf-8");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");

        //初始连接池大小
        dataSource.setInitialSize(2);

        //最大连接数量
        dataSource.setMaxIdle(10);
    }

    public static Connection getConnection() {
        System.out.println("从连接池中获取连接");
        try {
            //从连接池中获取连接
            return dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


}
